package view.backend.customer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import model.CustomerSpecified;
import view.ViewHandler;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminCustomerPanel extends JPanel
{
    public AdminCustomerPanel(final AdminCustomer view, final CustomerSpecified customer)
    {
        setLayout(null);
        setSize(new Dimension(260, 130));
        setBorder(DesignLayout.T_FIELD_RED);
        
        JLabel lblPhone = new JLabel(customer.getCustomerTelephone().getNumber());
        lblPhone.setBounds(10, 5, 250, 18);
        lblPhone.setFont(DesignLayout.STANDARD);
        
        JLabel lblName = new JLabel("<html>"+customer.getCustomerName().getName()+" (<b>ID:</b> "+customer.getCustomerName().getId()+")</html>");
        lblName.setBounds(10, 25, 250, 18);
        if (customer.getCustomerName().getName().length() > 30)
        {
            lblName.setFont(DesignLayout.STANDARD.deriveFont(12f));
        }
        else
        {
            lblName.setFont(DesignLayout.STANDARD);
        }        
        
        JLabel lblStreet = new JLabel("");
        lblStreet.setBounds(10, 45, 250, 18);
        lblStreet.setFont(DesignLayout.STANDARD);
        
        JLabel lblZipCity = new JLabel("");
        lblZipCity.setBounds(10, 65, 250, 18);
        lblZipCity.setFont(DesignLayout.STANDARD);
        
        if (customer.getCustomerAddress() != null)
        {
            lblStreet.setText(customer.getCustomerAddress().getStreet());
            lblZipCity.setText(customer.getCustomerAddress().getZipCode().getZipCode()+" "+customer.getCustomerAddress().getZipCode().getCity());
        }
        
        JButton btnAddName = new JButton("navn");
        btnAddName.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddName.setBorder(DesignLayout.T_FIELD_RED);
        btnAddName.setBackground(Color.lightGray);
        btnAddName.setPreferredSize(new Dimension(50, 30));
        btnAddName.setEnabled(false);
        
        JButton btnAddPhone = new JButton("nummer");
        btnAddPhone.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddPhone.setBorder(DesignLayout.T_FIELD_RED);
        btnAddPhone.setBackground(Color.lightGray);
        btnAddPhone.setPreferredSize(new Dimension(65, 30));
        btnAddPhone.setEnabled(false);
        
        JButton btnAddAddress = new JButton("adresse");
        btnAddAddress.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnAddAddress.setBorder(DesignLayout.T_FIELD_RED);
        btnAddAddress.setBackground(Color.lightGray);
        btnAddAddress.setPreferredSize(new Dimension(65, 30));
        btnAddAddress.setEnabled(false);
        
        JButton btnOrder = new JButton("ordre");
        btnOrder.setFont(DesignLayout.STANDARD.deriveFont(11f));
        btnOrder.setBorder(DesignLayout.T_FIELD_RED);
        btnOrder.setBackground(Color.lightGray);
        btnOrder.setPreferredSize(new Dimension(50, 30));
        btnOrder.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                // Reset top menu background colors
                ViewSingleton.getSingletonObject().getMenus()[0].resetBackgroundColor(DesignLayout.T_FIELD_BORDER_RED);
                            
                ViewHandler.toggleAllsView("adminCustomer", false);
                ViewHandler.toggleAllsView("menuLeft", false);
                ViewHandler.toggleAllsView("orderContainer", true);
                
                if (ViewSingleton.getSingletonObject().getOrderCustomer() != null)
                {
                    ViewSingleton.getSingletonObject().getOrderCustomer().settFieldNumber(customer.getCustomerTelephone().getNumber());
                    CustomerSpecified[] toSearchFor = { customer };
                    ViewSingleton.getSingletonObject().getOrderCustomer().getOrderCustomerList().buildFoundPanel(toSearchFor);
                }
            }
        });
        
        if (customer.getCustomerAddress() == null)
        {
            btnOrder.setEnabled(false);
        }
        
        add(lblPhone);
        add(lblName);
        add(lblStreet);
        add(lblZipCity);
        
        JPanel panelButtons = new JPanel();
        panelButtons.setBounds(4, 90, 250, 35);
        panelButtons.add(btnAddName);
        panelButtons.add(btnAddPhone);
        panelButtons.add(btnAddAddress);
        panelButtons.add(btnOrder);
        
        add(panelButtons);
        
        MouseAdapter 
                    mouseEntered,
                    mouseExited;
        
        mouseEntered =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseEntered(MouseEvent evt)
                        {
                            setCursor(new Cursor(Cursor.HAND_CURSOR));
                        }
                    }
                );
        
        mouseExited =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseExited(MouseEvent evt)
                        {
                            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                        }
                    }
                );
        
        this.addMouseListener(mouseEntered);
        this.addMouseListener(mouseExited);
    }
}